#include "newbilldialog.h"
#include "ui_newbilldialog.h"
#include <QtSql>
#include <QInputDialog>
#include <QMessageBox>

NewBillDialog::NewBillDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NewBillDialog)
{
    ui->setupUi(this);
    created=false;
}

NewBillDialog::~NewBillDialog()
{
    delete ui;
}

void NewBillDialog::on_buttonBox_accepted()
{
    QSqlQuery q;
    if(ui->lineEdit->text().isEmpty()){
        QMessageBox::warning(this, tr("Pig Bills"), tr("The Bill name cannot be null!"));
        return;
    }
    q.prepare("select bill_header_id from pig_bill_headers where name=:name");
    q.bindValue(":name",ui->lineEdit->text());
    q.exec();
    if (!q.isActive()) {
        QMessageBox::warning(this, tr("Pig Bills"), tr("System Error!"));
        return;
    }
    if (q.next()) {
        QMessageBox::warning(this, tr("Pig Bills"), tr("The name you entered is already exists!"));
        return;
    }
    q.prepare("insert into pig_bill_headers (name,budget,amount,last_modified_date) values (:name,:budget,0,datetime('now'))");
    q.bindValue(":name",ui->lineEdit->text());
    q.bindValue(":budget",ui->doubleSpinBox->value());
    q.exec();
    created=true;
}
